﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectNocturnalFool
{
    internal class Util
    {
        /// <summary>
        /// Finds the x point to align 2 objects
        /// </summary>
        /// <param name="x1">Objects to align to x position</param>
        /// <param name="width1">Width of align to object</param>
        /// <param name="width2">Width of aligned object</param>
        /// <returns>X position of object</returns>
        public static int getCentreDrawPointX(int x1, int width1, int width2)
        {
            int centreX = x1 + width1 / 2;
            
            return centreX - width2/2;
        }

        /// <summary>
        /// Finds the y point to align 2 objects
        /// </summary>
        /// <param name="x1">Objects to align to y position</param>
        /// <param name="width1">Width of align to object</param>
        /// <param name="width2">Width of aligned object</param>
        /// <returns>X position of object</returns>
        public static int getCentreDrawPointY(int y1, int height1, int height2)
        {
            int centreY = y1 + height1/ 2;

            return centreY - height2 / 2;
        }

        /// <summary>
        /// Splits a string to fit the width, will increase height
        /// </summary>
        /// <param name="message">String to be measured</param>
        /// <param name="ctrlWidth">Width of the control</param>
        /// <returns>String split with \n</returns>
        public static string splitString(string message, int ctrlWidth)
        {
            String line = String.Empty;
            String returnString = String.Empty;
            String[] wordArray = message.Split(' ');

            foreach (String word in wordArray)
            {
                if (TacticsGame.Instance.gameFont.MeasureString(line + word).Length() > ctrlWidth)
                {
                    if (line != "")
                    {
                        returnString = returnString + line + '\n';
                        line = String.Empty;
                    }
                    else
                    {
                        returnString = returnString + word + '\n';
                    }

                }

                line = line + word;
            }

            return returnString + line;
        }

        /// <summary>
        /// Draws a border around a rectangle
        /// </summary>
        /// <param name="rect">Rectangle to draw border</param>
        /// <!---->SPRITE DRAW MUST ALREADY BE CALLED
        public static void drawBorder(Rectangle rect, Color color, int thickness)
        {
            Texture2D pixel = new Texture2D(TacticsGame.Instance.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            pixel.SetData(new[] { color });            

            TacticsGame.Instance.spriteBatch.Draw(pixel, new Rectangle(rect.X,                          rect.Y,                             rect.Width,     thickness),     Color.White);
            TacticsGame.Instance.spriteBatch.Draw(pixel, new Rectangle(rect.X,                          rect.Y + rect.Height - thickness,   rect.Width,     thickness),     Color.White);
            TacticsGame.Instance.spriteBatch.Draw(pixel, new Rectangle(rect.X,                          rect.Y,                             thickness,      rect.Height),   Color.White);
            TacticsGame.Instance.spriteBatch.Draw(pixel, new Rectangle(rect.X + rect.Width - thickness, rect.Y,                             thickness,      rect.Height),   Color.White);

            pixel.Dispose();
        }

        public static int[] spaceEvenX(int numObjects, int widthOfObject, int widthOfSpace)
        {

            float spacing = widthOfSpace - (numObjects * widthOfObject);
            spacing = spacing / (numObjects + 1);

            int[] xPos;
            xPos = new int[numObjects];

            for (int i = 0; i < numObjects; i++)
            {
                if (i != 0)
                    xPos[i] = xPos[i - 1] + (int)spacing + widthOfObject;
                else
                    xPos[i] = (int)spacing;

            }

            return xPos;
        }

    }
}
